L.WMS.Tooltip = L.Class.extend({

    // @section Methods for modifying draw state

    // @method initialize(map): void
    // Tooltip constructor
    initialize: function (map) {
        this._map = map;
        this._popupPane = map._panes.popupPane;
        this._visible = false;

        this._container = map.options.drawControlTooltips ?
            L.DomUtil.create('div', 'leaflet-wms-tooltip', this._popupPane) : null;
        this._singleLineLabel = false;

        this._map.on('mouseout', this._onMouseOut, this);
        this._container.onmouseenter = this._onMouseEnterOfDiv;
        this._container.onmouseleave = this._onMouseLeaveOfDiv;
    },

    // @method dispose(): void
    // Remove Tooltip DOM and unbind events
    dispose: function () {
        this._map.off('mouseout', this._onMouseOut, this);

        if (this._container) {
            this._popupPane.removeChild(this._container);
            this._container = null;
        }
    },

    // @method updateContent(labelText): this
    // Changes the tooltip text to string in function call
    updateContent: function (htmlContent, latlng) {
        if (!this._container) {
            return this;
        }

        this._container.innerHTML = htmlContent;

        if (!htmlContent) {
            this._visible = false;
            this._container.style.visibility = 'hidden';
        } else {
            this._visible = true;
            this._container.style.visibility = 'inherit';
        }

        if (latlng){
            this.updatePosition(latlng);
        }

        return this;
    },

    // @method updatePosition(latlng): this
    // Changes the location of the tooltip
    updatePosition: function (latlng) {
        var pos = this._map.latLngToLayerPoint(latlng),
            tooltipContainer = this._container;

        if (this._container) {
            if (this._visible) {
                tooltipContainer.style.visibility = 'inherit';
            }
            L.DomUtil.setPosition(tooltipContainer, pos);
        }

        return this;
    },

    _onMouseOut: function () {
        if (this._container) {
            this._container.style.visibility = 'hidden';
        }
    },

    // 鼠标移入div取消查询，tooltip不消失
    _onMouseEnterOfDiv: function (){
        this.originIdentifyState = enableIdentify;
        enableIdentify = false;
    },

    // 鼠标移出div隐藏tooltip，恢复查询状态
    _onMouseLeaveOfDiv: function (){
        enableIdentify = this.originIdentifyState;
        this.style.visibility = 'hidden';
    }
});